package com.gitee.hljdrl.dialog.visitor;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;

import com.gitee.hljdrl.dialog.R;
import com.gitee.hljdrl.dialog.UIDialog;
import com.gitee.hljdrl.dialog.util.UIUtil;


public class ProgressVisitor extends BasisViewPartVisitor {

    public static final int STYLE_WHITE = 1;
    public static final int STYLE_BLACK = 2;
    private TextView mTvLoad;
    private String mProgressText;
    //------------------------------------------
    private int mStyle = STYLE_BLACK;
    private int mBackGround = 0;
    private int mTextColor = 0;

    public ProgressVisitor(Context context) {
        super(context);
        setLayout(R.layout.dialog_progress_part);
        setDialogTheme(R.style.UIDialogNetLoad);
    }

    @Override
    protected boolean enablePromptVisit() {
        return false;
    }

    @Override
    public void onViewVisit(Dialog dialog, View view) {
        setStatusBarTextColor(dialog.getContext(), dialog.getWindow());
        //
        mTvLoad = view.findViewById(R.id.dialog_tv_title);
        if (!TextUtils.isEmpty(mProgressText)) {
            mTvLoad.setText(mProgressText);
        }
        if (mStyle == STYLE_BLACK) {
            view.setBackgroundResource(R.drawable.dialog_progress_black_background);
            mTvLoad.setTextColor(Color.WHITE);
        } else if (mStyle == STYLE_WHITE) {
            view.setBackgroundResource(R.drawable.dialog_progress_white_background);
            mTvLoad.setTextColor(Color.BLACK);
        } else {
            if (mBackGround != 0) {
                view.setBackgroundResource(mBackGround);
            }
            if (mTextColor != 0) {
                mTvLoad.setTextColor(mTextColor);
            }
        }
        if (mTimeDismiss > 0) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    UIUtil.dismissDialog(dialog);
                }
            }, mTimeDismiss);
        }
        super.onViewVisit(dialog, view);
    }

    void setStatusBarTextColor(Context context, Window window) {
        if (window != null) {
            // 设置状态栏颜色
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(context.getResources().getColor(android.R.color.white));
            // 设置状态栏字体颜色为黑色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int flags = window.getDecorView().getSystemUiVisibility();
                flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                window.getDecorView().setSystemUiVisibility(flags);
            }
        }
    }

    public ProgressVisitor setTimeDismiss(long time) {
        this.mTimeDismiss = time;
        return this;
    }

    public ProgressVisitor setTextColor(@ColorInt int textColor) {
        this.mTextColor = textColor;
        return this;
    }

    public ProgressVisitor setBackground(@DrawableRes int background) {
        this.mBackGround = background;
        return this;
    }

    public ProgressVisitor setStyle(int style) {
        this.mStyle = style;
        return this;
    }

    public ProgressVisitor setProgressText(String text) {
        this.mProgressText = text;
        return this;
    }

    public ProgressVisitor updateProgressText(String text) {
        if (mTvLoad != null) {
            mTvLoad.setText(text);
        }
        return this;
    }

    @Override
    protected Dialog createDialog(Context context, int theme) {
        UIDialog dialog = new UIDialog(context, theme);
        dialog.setViewPartVisitor(this);
        return dialog;
    }

}
